<template>
  <a-card :bordered='false' :class="'cust-erp-sub-tab'">
    <!-- 操作按钮区域 -->
    <div class='table-operator' v-if='mainId'>
      <a-button @click='handleAdd' type='primary' icon='plus'>新增</a-button>
      <!--      <a-button type="primary" icon="download" @click="handleExportXls('任务表')">导出</a-button>-->
      <!--      <a-upload-->
      <!--        name="file"-->
      <!--        :showUploadList="false"-->
      <!--        :multiple="false"-->
      <!--        :headers="tokenHeader"-->
      <!--        :action="importExcelUrl"-->
      <!--        @change="handleImportExcel">-->
      <!--          <a-button type="primary" icon="import">导入</a-button>-->
      <!--      </a-upload>-->
      <!-- 高级查询区域 -->
      <!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if='selectedRowKeys.length > 0'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='batchDel'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'> 批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class='ant-alert ant-alert-info' style='margin-bottom: 16px;'>
        <i class='anticon anticon-info-circle ant-alert-icon'></i> 已选择 <a
        style='font-weight: 600'>{{ selectedRowKeys.length }}</a>项
        <a style='margin-left: 24px' @click='onClearSelected'>清空</a>
      </div>

      <a-table
        ref='table'
        size='middle'
        bordered
        rowKey='id'
        :scroll='{x:true}'
        :columns='columns'
        :dataSource='dataSource'
        :pagination='ipagination'
        :loading='loading'
        :rowSelection='{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}'
        @change='handleTableChange'>

        <template slot='htmlSlot' slot-scope='text'>
          <div v-html='text'></div>
        </template>
        <template slot='imgSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无图片</span>
          <img v-else :src='getImgView(text)' height='25px' alt=''
               style='max-width:80px;font-size: 12px;font-style: italic;' />
        </template>
        <template slot='fileSlot' slot-scope='text'>
          <span v-if='!text' style='font-size: 12px;font-style: italic;'>无文件</span>
          <a-button
            v-else
            :ghost='true'
            type='primary'
            icon='download'
            size='small'
            @click='downloadFile(text)'>
            下载
          </a-button>
        </template>
        <template slot='taskDes' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <template slot='incompleteCause' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <template slot='completeCause' slot-scope='text'>
          <j-ellipsis :value='text' :length='10' />
        </template>
        <span slot='action' slot-scope='text, record'>
          <a @click='handleEdit(record)'>编辑</a>
          <!--          <a-divider type="vertical" />-->
          <!--          <a-dropdown>-->
          <!--            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>-->
          <!--            <a-menu slot="overlay">-->
          <!--              <a-menu-item>-->
          <!--                <a @click="handleDetail(record)">详情</a>-->
          <!--              </a-menu-item>-->
          <!--              <a-menu-item>-->
          <!--                <a @click="taskStart(record)">检查</a>-->
          <!--              </a-menu-item>-->
          <!--              <a-menu-item>-->
          <!--                <a @click="taskEnd(record)">处理</a>-->
          <!--              </a-menu-item>-->
          <!--              <a-menu-item>-->
          <!--                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">-->
          <!--                  <a>删除</a>-->
          <!--                </a-popconfirm>-->
          <!--              </a-menu-item>-->
          <!--            </a-menu>-->
          <!--          </a-dropdown>-->
        </span>

      </a-table>
    </div>

    <hfBussinessProjectTasks-modal ref='modalForm' @ok='modalFormOk' :mainId='mainId'></hfBussinessProjectTasks-modal>
  </a-card>
</template>

<script>

import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import HfBussinessProjectTasksModal from './modules/HfBussinessProjectTasksModal'
import { getAction } from '@api/manage'
import store from '@/store'


export default {
  name: 'HfBussinessProjectTasksList',
  mixins: [JeecgListMixin],
  components: { HfBussinessProjectTasksModal },
  props: {
    mainId: {
      type: String,
      default: '',
      required: false
    },
    planType: {
      type: String,
      default: '',
      required: false
    }
  },
  watch: {
    mainId: {
      immediate: true,
      handler(val) {
        if (!this.mainId) {
          this.clearList()
        } else {
          this.queryParam['planId'] = val
          this.loadData(1)
        }
      }
    }

  },
  data() {
    return {
      description: '双周计划表管理页面',
      disableMixinCreated: true,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '商机名称',
          align: 'center',
          dataIndex: 'opportunityName'
        },
        {
          title: '客户名称',
          align: 'center',
          dataIndex: 'shortName'
        },
        {
          title: '任务描述',
          align: 'center',
          dataIndex: 'taskDes',
          scopedSlots: { customRender: 'taskDes' },
          width: 300
        },
        {
          title: '任务状态',
          align: 'center',
          dataIndex: 'taskState_dictText'
        },
        // {
        //   title:'责任人',
        //   align:"center",
        //   dataIndex: 'dutyPersionId'
        // },
        // {
        //   title:'检查人',
        //   align:"center",
        //   dataIndex: 'examiner'
        // },
        {
          title: '未完成原因',
          align: 'center',
          dataIndex: 'incompleteCause',
          scopedSlots: { customRender: 'incompleteCause' },
          customRender: function(text) {
            return !text ? '---' : text
          }
        },
        {
          title: '完成情况',
          align: 'center',
          dataIndex: 'completeCause',
          scopedSlots: { customRender: 'completeCause' },
          customRender: function(text) {
            return !text ? '---' : text
          }
        },
        // {
        //   title:'创建时间',
        //   align:"center",
        //   dataIndex: 'createTime',
        //   customRender:function (text) {
        //     return !text?"":(text.length>10?text.substr(0,10):text)
        //   }
        // },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/bussiness/hfBussinessDoubleWeekPlan/listHfBussinessProjectTasksByMainId',
        delete: '/bussiness/hfBussinessDoubleWeekPlan/deleteHfBussinessProjectTasks',
        deleteBatch: '/bussiness/hfBussinessDoubleWeekPlan/deleteBatchHfBussinessProjectTasks',
        exportXlsUrl: '/bussiness/hfBussinessDoubleWeekPlan/exportHfBussinessProjectTasks',
        importUrl: '/bussiness/hfBussinessDoubleWeekPlan/importHfBussinessProjectTasks',
        changeTaskStatus: 'bussiness/hfBussinessDoubleWeekPlan/changeTaskStatus'
      },
      dictOptions: {
        subjectId: [],
        planType: []
      },
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl() {
      return `${window._CONFIG['domianURL']}/${this.url.importUrl}/${this.mainId}`
    }
  },
  methods: {
    clearList() {
      this.dataSource = []
      this.selectedRowKeys = []
      this.ipagination.current = 1
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'deleteFlag', text: '删除标记', dictCode: '' })
      fieldList.push({
        type: 'popup',
        value: 'projectId',
        text: '项目简称',
        popup: { code: 'hf_pm_project', field: 'short_name', orgFields: 'short_name', destFields: 'project_id' }
      })
      fieldList.push({ type: 'string', value: 'subjectId', text: '专题名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'targetId', text: '目标名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'planType', text: '计划类型', dictCode: '' })
      fieldList.push({ type: 'date', value: 'startTime', text: '开始时间' })
      fieldList.push({ type: 'date', value: 'endTime', text: '结束时间' })
      this.superFieldList = fieldList
    },

    handleEdit: function(record) {
      if (record.taskState == '4' || record.taskState == '3' || this.planType == '2') {
        this.$message.error('请选择未开始状态的任务或者未归档的计划进行编辑!')
        return
      }
      let isAdd = '1'
      this.$refs.modalForm.isAdd = isAdd
      this.$refs.modalForm.planType = this.planType
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = false
    },
    handleDetail: function(record) {
      this.isView = '1'
      this.$refs.modalForm.edit(record)
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
    },
    handleAdd: function() {
      if (this.planType == '2') {
        this.$message.error('请选择未归档的计划进行新增!')
        return
      }
      let record = {
        dutyPersionId: store.getters.userInfo.realname,
        taskState: '1'
      }
      let isAdd = '2'
      this.$refs.modalForm.isAdd = isAdd
      this.$refs.modalForm.addRecord(record)
      this.$refs.modalForm.title = '新增'
      this.$refs.modalForm.disableSubmit = false
    },
    taskStart: function(record) {
      if ('1' != record.taskState) {
        this.$message.error('请操作未开始的任务!')
        return
      }
      record.changeTaskStatus = '2'
      getAction(this.url.changeTaskStatus, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success('任务已经开始。')
        } else {
          this.$message.warning('任务开始操作失败！')
        }
      })
    },
    taskEnd: function(record) {
      if ('2' != record.taskState) {
        this.$message.error('请操作进行中的任务!')
        return
      }
      record.changeTaskStatus = '4'
      getAction(this.url.changeTaskStatus, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success('任务已经完成。')
        } else {
          this.$message.warning('任务完成操作失败！')
        }
      })
    },
    taskEx: function(record) {
      if ('2' != record.taskState) {
        this.$message.error('请操作进行中的任务!')
        return
      }
      record.changeTaskStatus = '3'
      getAction(this.url.changeTaskStatus, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success('任务状态已经修改为异常。')
        } else {
          this.$message.warning('任务异常处理失败！')
        }
      })
    },
    taskClose: function(record) {
      if (!this.url.changeTaskStatus) {
        this.$message.error('请设置url.changeTaskStatus属性!')
      }
      getAction(this.url.changeTaskStatus, record).then((res) => {
        if (res.success) {
          this.loadData()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less'
</style>
